<?php
/**
 * 真题管理
 * Created by PhpStorm.
 * User: Administrator
 * Date: 15-6-17
 * Time: 下午2:37
 */
namespace app\modules\test\controllers;

use app\libs\Method;
use app\modules\test\models\TestCate;
use app\modules\test\models\TestPublic;
use yii;
use app\libs\ApiControl;

class ResultController extends ApiControl
{
    public $layout = 'test';
    public $enableCsrfValidation = false;

    public function actionIndex()
    {
        $page = Yii::$app->request->get('page', 1);
        //用户ID 电话（关联的手机号）模考日期 模考类别：听力、阅读、全套 模考编号 模考名称 模考状态：未完成、已完成 得分
        $data = TestPublic::find()->from('x2_test_public mp')->select('mp.id,mp.createTime,mp.cateId,mp.subjectId,mp.uid,mp.status,mp.score,u.phone,u.userName')->asArray()->leftJoin('x2_user u', 'u.uid=mp.uid')->offset(($page - 1) * 20)->limit(20)->orderBy('id desc')->all();
        $count = TestPublic::find()->select('id')->count();
        $page = Method::getPagedRows(['count' => $count, 'pageSize' => 20, 'rows' => 'models']);
        foreach ($data as $k => $v) {
            $cate = TestCate::find()->asArray()->where('id=' . $v['cateId'])->one();
            $subject = $v['subjectId'] != false ? TestCate::find()->asArray()->select('group_concat(name) as subjectName')->where('id in (' . $v['subjectId'] . ')')->one() : '';
            $data[$k]['cateName'] = $cate['name'];
            $data[$k]['subjectName'] = $subject != false ? $subject['subjectName'] : '';
        }

        return $this->render('index', ['data' => $data, 'page' => $page]);
    }


}